export class Character {
    constructor(config) {
        this.id = config.id || Math.random().toString(36).substr(2, 9);
        this.name = config.name || 'Unknown Character';
        this.maxHealth = config.maxHealth || 80;
        this.health = config.health !== undefined ? config.health : this.maxHealth;
        this.block = config.block || 0;
        this.energy = config.energy || 3;
        this.maxEnergy = config.maxEnergy || 3;
        this.deck = config.deck || [];
        this.drawPile = [];
        this.hand = [];
        this.discardPile = [];
        this.exhaustPile = [];
        this.relics = config.relics || [];
        this.statusEffects = {};
    }

    startTurn() {
        // 重置能量
        this.energy = this.maxEnergy;
        
        // 抽牌
        this.drawCards(5);
        
        // 处理回合开始时的状态效果
        this.processStartOfTurnEffects();
    }

    endTurn() {
        // 丢弃手牌
        this.discardHand();
        
        // 处理回合结束时的状态效果
        this.processEndOfTurnEffects();
    }

    drawCards(count) {
        for (let i = 0; i < count; i++) {
            if (this.drawPile.length === 0) {
                // 如果抽牌堆为空，将弃牌堆洗入抽牌堆
                if (this.discardPile.length === 0) {
                    // 如果弃牌堆也为空，则无法抽牌
                    break;
                }
                this.reshuffleDiscardPile();
            }
            
            // 从抽牌堆顶抽一张牌
            const card = this.drawPile.pop();
            this.hand.push(card);
        }
    }

    reshuffleDiscardPile() {
        // 将弃牌堆洗入抽牌堆
        this.drawPile = [...this.discardPile];
        this.discardPile = [];
        
        // 洗牌
        this.shuffleDeck(this.drawPile);
    }

    shuffleDeck(deck) {
        // 洗牌算法
        for (let i = deck.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [deck[i], deck[j]] = [deck[j], deck[i]];
        }
    }

    discardHand() {
        // 将手牌移到弃牌堆
        this.discardPile = [...this.discardPile, ...this.hand];
        this.hand = [];
    }

    playCard(cardIndex, target) {
        if (cardIndex < 0 || cardIndex >= this.hand.length) {
            return false;
        }
        
        const card = this.hand[cardIndex];
        
        // 检查能量是否足够
        if (card.cost > this.energy) {
            return false;
        }
        
        // 消耗能量
        this.energy -= card.cost;
        
        // 打出卡牌
        const success = card.play(target, this);
        
        if (success) {
            // 从手牌移除
            this.hand.splice(cardIndex, 1);
            
            // 根据卡牌类型决定去向
            if (card.exhaust) {
                this.exhaustPile.push(card);
            } else {
                this.discardPile.push(card);
            }
        }
        
        return success;
    }

    takeDamage(amount) {
        // 先减少格挡
        if (this.block > 0) {
            if (amount <= this.block) {
                this.block -= amount;
                amount = 0;
            } else {
                amount -= this.block;
                this.block = 0;
            }
        }
        
        // 再减少生命值
        if (amount > 0) {
            this.health -= amount;
            if (this.health < 0) this.health = 0;
        }
        
        return amount;
    }

    addBlock(amount) {
        this.block += amount;
        return this.block;
    }

    heal(amount) {
        this.health += amount;
        if (this.health > this.maxHealth) {
            this.health = this.maxHealth;
        }
        return this.health;
    }

    addStatus(statusType, amount) {
        if (!this.statusEffects[statusType]) {
            this.statusEffects[statusType] = 0;
        }
        this.statusEffects[statusType] += amount;
    }

    processStartOfTurnEffects() {
        // 处理回合开始时的状态效果
        // 例如减少某些状态效果的持续时间
    }

    processEndOfTurnEffects() {
        // 处理回合结束时的状态效果
        // 例如受到中毒伤害
        if (this.statusEffects.poison > 0) {
            this.takeDamage(this.statusEffects.poison);
            this.statusEffects.poison--;
        }
        
        // 减少状态效果持续时间
        for (const status in this.statusEffects) {
            if (this.statusEffects[status] > 0) {
                this.statusEffects[status]--;
            }
        }
    }

    isDead() {
        return this.health <= 0;
    }
}